<template>
  <div>
    <div class="app-container pageHeader">
      <div class="header">{{$route.meta.title}}-监管科</div>
    </div>

    <div class="app-container margin-content padding-content">
      <el-form
        label-position="left"
        :model="dataForm"
        ref="dataForm"
        label-width="120px">

        <el-row :gutter="48">
          <el-col :sm="24" :md="8">
            <el-form-item label="提运单号" prop="blNo">
              <el-input placeholder="提运单号" v-model="dataForm.blNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="集装箱作业时间" prop="time">
              <el-date-picker
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                unlink-panels
                value-format="yyyy-MM-dd"
                v-model="dataForm.time"
                :picker-options="pickerOptions"
                style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="审核状态">
              <el-select clearable v-model="dataForm.auditStatus" style="width: 100%;">
                <el-option
                  v-for="item in auditStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="报关（检）单号">
              <el-input v-model="dataForm.declNo" placeholder="报关（检）单号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="16" style="text-align:right">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="app-container margin-content padding-content">
      <div class="btn-wrapper">
        <!--审核 审核状态（0：未审核，1：已审核，2：退回）-->
        <el-button type="primary" icon="el-icon-news" @click="authHandle({readyIds:ids, auditStatus:1})">确认</el-button>
        <el-button type="danger" icon="el-icon-refresh" @click="authHandle({readyIds:ids, auditStatus:2})">退回
        </el-button>
        <el-button icon="el-icon-news" @click="goToAudit">送审</el-button>
      </div>

      <el-table
        ref="confirmTable"
        :key="tableKey"
        :data="tableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        :highlight-current-row="true"
        @selection-change="handleSelectionChange"
        style="width: 100%;"
        :max-height="scrollHeight">

        <el-table-column type="selection" align="center" width="35" :selectable="_selectable"></el-table-column>
        <el-table-column label="报关（检）单号" property="declNo" width="150" fixed="left"></el-table-column>
        <el-table-column label="查验来源" property="checkSource">
          <template slot-scope="scope">
            {{scope.row.checkSource | typeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="提运单号" property="blNo" width="130"></el-table-column>
        <el-table-column label="集装箱号" property="contNo" width="100"></el-table-column>
        <el-table-column label="码头作业时间" property="readyTime" width="130"></el-table-column>
        <el-table-column label="集装箱尺寸" property="contSize"></el-table-column>
        <el-table-column label="进/出口标记" property="ieFlag" width="90">
          <template slot-scope="scope">
            {{scope.row.ieFlag | ieFlagFilter}}
          </template>
        </el-table-column>
        <el-table-column label="申报口岸" property="declPort" width="120">
          <template slot-scope="scope">
            {{scope.row.declPort | portCodeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="查验方式" property="checkType">
          <template slot-scope="scope">
            {{scope.row.checkType | checkTypeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="码头作业类型" property="actType" width="200">
          <template slot-scope="scope">
            {{scope.row.actType | chkReadyFilter}}
          </template>
        </el-table-column>
        <el-table-column label="集装箱位置" property="location"></el-table-column>
        <el-table-column label="操作联系人" property="contact"></el-table-column>
        <el-table-column label="操作联系人电话" property="contactTel" width="120"></el-table-column>
        <el-table-column label="审核状态" property="auditStatus" fixed="right">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.auditStatus === '1'" type="success" fiexe="right">
              {{scope.row.auditStatus | auditStatusFilter}}
            </el-tag>
            <el-tag v-else-if="scope.row.auditStatus === '2'" type="danger">
              {{scope.row.auditStatus | auditStatusFilter}}
            </el-tag>
            <el-tag v-else type="warning">
              {{scope.row.auditStatus | auditStatusFilter}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <div v-if="parseInt(scope.row.cusSubmit) !== 1">
              <a @click="authHandle({readyIds: scope.row.readyId, auditStatus: 1})">确认</a>
              <divider type="vertical"></divider>
              <a @click="authHandle({readyIds: scope.row.readyId, auditStatus: 2})">退回</a>
            </div>
            <div v-else style="color:#ccc">暂无操作</div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container align-right" v-show="tableData.length > 0">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="dataForm.pageNum"
          :page-sizes="[10,20,30,50]"
          :page-size="dataForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗, 审核 -->
    <audit v-if="auditVisible" type="confirm" ref="audit" @refresh="_getAuditOrders()"></audit>
  </div>
</template>

<script>
  // api
  import { getAuditOrders } from '@/api/statistics'
  // utils
  import { auditStatus } from '@/utils/selecterOptions'
  import { pickerOptions, getLately, pickerOptionsMonths } from '@/utils'
  import { getMonth } from '@/utils/tools'
  // vuex
  import { mapMutations } from 'vuex'
  // components
  import Divider from '@/components/Divider'
  import Audit from '@/components/Audit'

  export default {
    name: 'costConfirm',
    data() {
      return {
        tableKey: 0,
        tableData: [],
        loading: false,
        scrollHeight: null,
        dataForm: {
          portId: null,
          containerId: '',
          blNo: '',
          time: [],
          auditStatus: '',
          declNo: '',
          pageNum: 1,
          pageSize: 20
        },
        total: 0,
        pickerOptions: {
          shortcuts: pickerOptionsMonths
        },
        multipleSelection: [],
        auditVisible: false,
        auditStatus: auditStatus
      }
    },
    computed: {
      ids() {
        return this.multipleSelection.join(',')
      }
    },
    created() {
      this._getAuditOrders()
      this.tableResize()
    },
    methods: {
      tableResize() {
        this.scrollHeight = document.body.scrollHeight - 505
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - 505
        }
      },
      _selectable(row, index) {
        if (parseInt(row.cusSubmit) !== 1) {
          return true
        } else {
          return false
        }
      },
      _getAuditOrders() {
        this.loading = true
        const [beginTime, endTime] = this.dataForm.time || ''
        const params = {beginTime, endTime}
        const data = Object.assign({}, params, this.dataForm)
        getAuditOrders(data).then((res) => {
          this.tableData = res.data.data.dataList
          this.total = res.data.data.total
          this.loading = false
        })
      },
      handleSizeChange(val) {
        this.dataForm.pageSize = val
        this._getAuditOrders()
      },
      handleCurrentChange(val) {
        this.dataForm.pageNum = val
        this._getAuditOrders()
      },
      // 审核 审核状态（0：未审核，1：确认查验，2：审核不通过）
      authHandle({readyIds, auditStatus}) {
        this.auditVisible = true
        this.$nextTick(() => {
          this.setCurrentId({readyIds, auditStatus})
          this.$refs['audit'].init()
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val.map((item) => {
          return item.readyId
        })
      },
      resetForm() {
        this.$refs['dataForm'].resetFields()
      },
      handleSearch() {
        this.dataForm.pageNum = 1
        this._getAuditOrders()
      },
      goToAudit() {
        location.href = '#/chkSys/chkCost/send'
      },
      ...mapMutations({
        setCurrentId: 'SET_CURRENTID'
      })
    },
    components: {
      Divider,
      Audit
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss">

</style>
